package com.sanjiahantang.youjiarole.module.complain.adapters;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyGridView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.common.beans.FileBean;
import com.sanjiahantang.youjiarole.module.complain.beans.ComplainBean;
import com.sanjiahantang.youjiarole.module.complain.views.MeComplainView;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.squareup.picasso.Picasso;


/**
 * Created by zy on 2017/9/11.
 */

public class ComplainAdapter extends RecyclerView.Adapter{

    private Context context;
    private LayoutInflater mLayoutInflater;
    private ComplainBean data;
    private int type;
    public ComplainAdapter(Context context, ComplainBean data, int type){
        this.context = context;
        this.mLayoutInflater = LayoutInflater.from(context);
        this.data = data;
        this.type = type;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Holder(context,mLayoutInflater.inflate(R.layout.item_me_complain,null),data.getData().getList().get(viewType));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return (data == null || data.getData() == null || data.getData().getList() == null) ? 0 : data.getData().getList().size();
    }

    private class Holder extends RecyclerView.ViewHolder{
        public Holder(Context context, View itemView, ComplainBean.Item data) {
            super(itemView);
            TextView txtCreateTime = (TextView) itemView.findViewById(R.id.txtCreateTime);
            TextView txtState = (TextView) itemView.findViewById(R.id.txtState);
            TextView txtReason = (TextView) itemView.findViewById(R.id.txtReason);
            TextView txtTargetName = (TextView) itemView.findViewById(R.id.txtTargetName);
            TextView txtTargetTag = (TextView) itemView.findViewById(R.id.txtTargetTag);

            txtCreateTime.setText(data.getComplaintTime().substring(0,"yyyy-MM-dd".length()));
            txtReason.setText(data.getComplaintContent());
            txtState.setText(MeComplainView.KEY_COMPLAIN_STATE[data.getComplaintState() - 1]);

            MyGridView gvImageList = (MyGridView) itemView.findViewById(R.id.gvImageList);
            if (data.getImgUrlList() == null || data.getImgUrlList().size() == 0){
                gvImageList.setVisibility(View.GONE);
            }else{
                gvImageList.setVisibility(View.VISIBLE);
                gvImageList.setAdapter(new QuickAdapter<FileBean.Data>(context,R.layout.item_image,data.getImgUrlList()) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, final FileBean.Data item) {
                        ImageView iv = helper.getView(R.id.image);
                        Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(R.drawable.i_rect_bg).into(iv);
                        helper.getView().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Utils.showPicDialog((Activity) context,FileUtils.rePath(item.getUrl()),R.drawable.i_rect_bg);
                            }
                        });
                    }
                });
            }
            if (type == MeComplainView.TYPE_TO_ME){
                txtTargetTag.setText("投诉人员");
                if (data.getComplaintName() != null){
                    txtTargetName.setText(data.getComplaintName());
                }else{
                    txtTargetName.setText("匿名");
                }
            }else{
                txtTargetTag.setText("被投诉人员");
                if (data.getComplaintObjectName() != null){
                    txtTargetName.setText(data.getComplaintObjectName());
                }else{
                    txtTargetName.setText("匿名");
                }
            }


        }


    }
}
